package ui.newui;

import ui.components.JJTable;

import javax.swing.*;
import java.awt.*;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * Created by IntelliJ IDEA.
 * User: [J0k3r]
 * Date: 29.11.2008
 * Time: 10:19:11
 */
public class Report extends JPanel {
    private JJTable tab = new JJTable();
    public Report(Integer type, Connection con) {
        this.setLayout(new BorderLayout(5, 5));
        this.add(tab, BorderLayout.CENTER);

        String sql = null;
        switch (type) {
            case 1: sql = "select count(1), db_object_template.object_template_name from db_object, db_object_template where db_object_template.id = db_object.db_object_template_id group by db_object_template.id";
            break;
            case 2: sql = "select db_object_template.`object_template_name`, db_object_param.`param_name`, db_object_param_value_int.`value` from\n" +
                    "       db_object, db_object_template, `db_object_param`, `db_object_param_value_int` /*, `db_object_param_value_string`*/\n" +
                    "where db_object_template.id = db_object.db_object_template_id\n" +
                    "      and db_object_param.`db_object_template_id` = db_object_template.id\n" +
                    "/*      and `db_object_param_value_string`.`db_object_param_id` = db_object_param.id*/\n" +
                    "      and `db_object_param_value_int`.`db_object_param_id` = db_object_param.id\n" +
                    "      group by db_object_param_value_int.`value`";
            break;
            case 3: sql = "";
            break;
            case 4: sql = "";
            break;
        }
        try {
            PreparedStatement statement = con.prepareStatement(sql);
            ResultSet resultSet = statement.executeQuery();
            for (int nI = 1; nI <= resultSet.getMetaData().getColumnCount(); ++nI) {
                tab.addColumn(resultSet.getMetaData().getColumnName(nI));
            }
            while (resultSet.next()) {
                Object [] rowData=  new Object[resultSet.getMetaData().getColumnCount()];
                for (int nI = 1; nI <= resultSet.getMetaData().getColumnCount(); ++nI)
                    rowData[nI - 1] = resultSet.getObject(nI);
                tab.addRow(null, rowData);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}